Conditions | 10 |
Paths | 20 |
Total Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like $(document).ready often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* Javascript interaction for PHP cheat sheets. */ |
||
49 | activate: function( event, ui ) { |
||
50 | var tableId, tabHref, tabTab, tabTitle, oldTab, oldTitle, titleText; |
||
51 | var oldTabNoUnderscore, oldTitlePre, oldUri; |
||
52 | |||
53 | /* (Re-)attach floating table headers for activated panel. */ |
||
54 | tableId = ui.newPanel.find( 'table' ).attr( 'id' ); |
||
55 | if ( tableId ) { |
||
56 | jQuery( '#' + tableId ).thfloat({ |
||
57 | side: 'head', |
||
58 | onShow: function( table, block ) { |
||
59 | |||
60 | // Remove hover and sticky classes as they will otherwise not stay consistent. |
||
61 | block.find( 'th' ).css( 'background', '' ); |
||
62 | } |
||
63 | }).thfloat({ |
||
64 | side: 'foot', |
||
65 | onShow: function( table, block ) { |
||
66 | |||
67 | // Remove hover and sticky classes as they will otherwise not stay consistent. |
||
68 | block.find( 'th' ).css( 'background', '' ); |
||
69 | } |
||
70 | }); |
||
71 | } |
||
72 | |||
73 | /* Remove hover and sticky classes as they will otherwise not stay consistent. */ |
||
74 | jQuery( '.hover, .sticky' ).removeClass( 'hover sticky' ); |
||
75 | |||
76 | |||
77 | /* Change the location bar url to the selected tab to enable reloading to the currently |
||
78 | selected tab and avoid the location bar change causing the page to reload. */ |
||
79 | tabHref = ui.newTab.find( 'a' ).attr( 'href' ); |
||
80 | tabTab = ui.newTab.find( 'a' ).attr( 'data-tab' ); |
||
81 | tabTitle = ui.newTab.find( 'a' ).attr( 'data-tab-title' ); |
||
82 | if ( tabHref && tabTab && tabTitle ) { |
||
1 ignored issue
–
show
|
|||
83 | oldTab = ui.oldTab.find( 'a' ).attr( 'data-tab' ); |
||
84 | oldTitle = ui.oldTab.find( 'a' ).attr( 'data-tab-title' ); |
||
85 | |||
86 | titleText = windowTitle.text(); |
||
87 | |||
88 | if ( titleText.indexOf( ':: ' + oldTitle ) !== -1 ) { |
||
89 | titleText = titleText.replace( ':: ' + oldTitle, ':: ' + tabTitle ); |
||
90 | } |
||
91 | else { |
||
92 | oldTabNoUnderscore = oldTab.replace( '_', ' ' ); |
||
93 | if ( titleText.indexOf( ':: ' + oldTabNoUnderscore ) !== -1 ) { |
||
94 | titleText = titleText.replace( ':: ' + oldTabNoUnderscore, ':: ' + tabTitle ); |
||
95 | } |
||
96 | // This was a generic call, no previous tab selected. |
||
97 | else { |
||
98 | oldTitlePre = titleText.substring( 0, titleText.indexOf( ' Cheatsheet for ' ) ); |
||
99 | titleText = oldTitlePre + titleText.replace( oldTitlePre, ( ' :: ' + tabTitle ) ); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | // Static sheets. |
||
104 | if ( '#' === tabHref.substring( 0, 1 ) ) { |
||
105 | oldUri = window.location.href; |
||
106 | if ( oldUri.indexOf( '#' ) !== -1 && window.location.hash === ( '#' + oldTab ) ) { |
||
107 | tabHref = oldUri.replace( window.location.hash, tabHref ); |
||
108 | } |
||
109 | else { |
||
110 | tabHref = oldUri + tabHref; |
||
111 | } |
||
112 | } |
||
113 | // Dynamic sheet. |
||
114 | else { |
||
115 | tabHref = tabHref.substring( 0, ( tabHref.indexOf( '/ajax' ) + 1 ) ); |
||
116 | } |
||
117 | |||
118 | // Add to history. |
||
119 | history.pushState( null, titleText, tabHref ); |
||
1 ignored issue
–
show
|
|||
120 | // Change the title bar title. |
||
121 | windowTitle.text( titleText ); |
||
122 | // Change the tab value for the php version dropdown. |
||
123 | formTabField.val( tabTab ); |
||
124 | } |
||
125 | }, |
||
126 | beforeLoad: function( event, ui ) { |
||
270 |
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
let
orconst
. These variables/constants are only valid in the code block where they have been declared.Consider the following two pieces of code:
and
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts.
To know more about this ECMA6 feature, look at the MDN pages on let and const.